package com.example.bookstore.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonView
import org.hibernate.annotations.DynamicInsert
import org.hibernate.annotations.DynamicUpdate
import org.hibernate.annotations.GenericGenerator
import java.util.*
import javax.persistence.*


/**
 * Created by IntelliJ IDEA.
 * User: lvshe
 * Date: 2019/7/24
 * Time: 12:08
 */
@DynamicInsert
@DynamicUpdate
@MappedSuperclass
open class BaseEntity {

    @Id
    @JsonView(BaseWithIdView::class)
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
    var id: String = ""


    @Version
    @JsonIgnore
    var version: Long = 0L

    @JsonIgnore
    lateinit var createTime: Date

    @JsonIgnore
    lateinit var updateTime: Date


    @PrePersist
    protected open fun prePersist() {
        this.createTime = Date()
        this.updateTime = Date()
    }

    @PreUpdate
    private fun preUpdate() {
        updateTime = Date()
    }

    interface BaseView
    interface BaseWithIdView
}
